home *** CD-ROM | disk | FTP | other *** search
/ Aminet 50 / Aminet 50 (2002)(GTI - Schatztruhe)[!][Aug 2002].iso / Aminet / dev / src / reactionexampl.lha / getfile.c < prev    next >
C/C++ Source or Header  |  2002-05-24  |  4KB  |  146 lines

  1. // getfile.c
  2. // Public Domain by Matthias Rustler
  3. // Use at your own risk
  4.  
  5. // Shows the use of a ReAction Getfile gadget
  6. // The gadged doesn't open the filerequester itself,
  7. // so we must open it with a method call in the message loop
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #define  ALL_REACTION_CLASSES
  13. #define  ALL_REACTION_MACROS
  14.  
  15. #include <reaction/reaction.h>
  16. #include <intuition/classusr.h>
  17. #include <proto/exec.h>
  18. #include <proto/intuition.h>
  19. #include <clib/alib_protos.h>
  20.  
  21. struct IntuitionBase *IntuitionBase;
  22. struct Library *GfxBase;
  23. struct Library *WindowBase;
  24. struct Library *LayoutBase;
  25. struct Library *StringBase;
  26. struct Library *GetFileBase;
  27.  
  28. BOOL alllibrariesopen = FALSE;
  29. Object *window, *layout, *filereq, *string;
  30.  
  31. #define GID_FILEREQ (1)
  32.  
  33. void cleanexit(char *str);
  34.  
  35. int main(void)
  36. {
  37.     char *str;
  38.     struct Window *intuiwin=NULL;
  39.     ULONG windowsignal,receivedsignal,result,code;
  40.     BOOL end;
  41.  
  42.     if ( ! (IntuitionBase= (struct IntuitionBase*)OpenLibrary("intuition.library",39)))
  43.         cleanexit("Can't open intuition.library");
  44.  
  45.     if ( ! (WindowBase= OpenLibrary("window.class",44)))
  46.         cleanexit("Can't open window.class");
  47.  
  48.     if ( ! (LayoutBase= OpenLibrary("gadgets/layout.gadget",44)))
  49.         cleanexit("Can't open layout.gadget");
  50.  
  51.     if ( ! (StringBase= OpenLibrary("gadgets/string.gadget",44)))
  52.         cleanexit("Can't open string.gadget");
  53.  
  54.     if ( ! (GetFileBase= OpenLibrary("gadgets/getfile.gadget",44)))
  55.         cleanexit("Can't open getfile.gadget");
  56.  
  57.     window = WindowObject,
  58.         WINDOW_Position, WPOS_CENTERSCREEN,
  59.         WA_Activate, TRUE,
  60.         WA_Title, "Getfile.gadget demo",
  61.         WA_DragBar, TRUE,
  62.         WA_CloseGadget, TRUE,
  63.         WA_DepthGadget, TRUE,
  64.         WA_SizeGadget, TRUE,
  65.         WA_IDCMP, IDCMP_CLOSEWINDOW|IDCMP_GADGETUP,
  66.         WINDOW_Layout, VLayoutObject,
  67.             LAYOUT_DeferLayout, TRUE,
  68.             LAYOUT_SpaceInner, TRUE,
  69.             LAYOUT_SpaceOuter, TRUE,
  70.             LAYOUT_AddChild, filereq = GetFileObject,
  71.                 GETFILE_TitleText , "GetFile.gadget Demo" ,
  72.                 GETFILE_DoPatterns , TRUE ,
  73.                 GA_ID , GID_FILEREQ ,
  74.                 GA_RelVerify , TRUE , // to get a message
  75.             End,
  76.             LAYOUT_AddChild, string = StringObject,
  77.                 STRINGA_MinVisible , 30 ,
  78.                 GA_ReadOnly , TRUE,
  79.             StringEnd,
  80.         LayoutEnd,
  81.     WindowEnd;
  82.  
  83.     if ( ! (window))
  84.         cleanexit("Can't create window");
  85.  
  86.     if ( ! (intuiwin = (struct Window *) DoMethod(window,WM_OPEN)))
  87.         cleanexit("Can't open window");
  88.  
  89.     GetAttr(WINDOW_SigMask,window,&windowsignal);
  90.  
  91.     end = FALSE;
  92.     while (!end)
  93.     {
  94.         receivedsignal = Wait(windowsignal);
  95.         while ((result = DoMethod(window,WM_HANDLEINPUT,&code)) != WMHI_LASTMSG)
  96.         {
  97.             switch (result & WMHI_CLASSMASK)
  98.             {
  99.                 case WMHI_CLOSEWINDOW:
  100.                     end=TRUE;
  101.                     break;
  102.                 case WMHI_GADGETUP:
  103.                     switch (result & WMHI_GADGETMASK)
  104.                     {
  105.                        case GID_FILEREQ:
  106.                             // Now we open the requester
  107.                             // The window parameter is important
  108.                             if (DoMethod(filereq , GFILE_REQUEST , intuiwin))
  109.                             {
  110.                                 GetAttr(GETFILE_FullFile , filereq ,(ULONG*)&str);
  111.                             }
  112.                             else
  113.                             {
  114.                                 str="Filerequester canceled";
  115.                             }
  116.                             // str can be an automatic variable, because the string gadget copies
  117.                             // the string into its buffer
  118.                             SetGadgetAttrs( (struct Gadget *)string , intuiwin, NULL,
  119.                                              STRINGA_TextVal , str , TAG_END);
  120.                             break;
  121.                     }
  122.                     break;
  123.             }
  124.         }
  125.     }
  126.     DoMethod(window,WM_CLOSE);
  127.     cleanexit(NULL);
  128. }
  129.  
  130. void cleanexit(char *str)
  131. {
  132.     if (str) printf("Error: %s\n",str);
  133.  
  134.     if (alllibrariesopen)
  135.     {
  136.         DisposeObject(window);
  137.     }
  138.  
  139.     CloseLibrary((struct Library*)IntuitionBase);
  140.     CloseLibrary(WindowBase);
  141.     CloseLibrary(LayoutBase);
  142.     CloseLibrary(StringBase);
  143.     exit(0);
  144. }
  145.  
  146.